package cn.hotel.utils;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import java.io.File;

public class uploadUntil {

    // 1 初始化用户身份信息(secretId, secretKey)
    static COSCredentials cred = new BasicCOSCredentials("AKIDoSwFKDuLbghWIx10wnQO8f2CarukLiqj", "PaXGrIagYJyXvESYbTArOOAiCeyA9vUo");
    // 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
    // clientConfig中包含了设置region, https(默认http), 超时, 代理等set方法, 使用可参见源码或者接口文档FAQ中说明
    static ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing"));
    // 3 生成cos客户端
    static COSClient cosClient = new COSClient(cred, clientConfig);
    // bucket的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式
    static String bucketName = "hotelimg-1318831475";

    public static String uploadImg(File file,String FileName){
//        File localFile = new File(filePath);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, FileName, file);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        String etag = putObjectResult.getETag();  // 获取文件的 etag
        return etag;
    }
}
